import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:yt_devops/components/app_permission/permission_widget.dart';
import 'package:yt_devops/components/log_utils.dart';

import 'package:yt_devops/components/paging/refresh.dart';
import 'package:yt_devops/pages/devices/device_list/device_list_model.dart';
import 'package:yt_devops/pages/devices/device_list/device_widget.dart';
import 'package:yt_devops/resources/resources.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/routers/router_path.dart';
import 'package:yt_devops/styles/styles.dart';
import 'package:yt_devops/user_entity_manager.dart';
import 'package:yt_devops/utils/constants/permission_page_constant.dart';
import 'package:yt_devops/utils/debounce_utils.dart';
import 'package:yt_devops/utils/enums.dart';
import 'device_list_controller.dart';

class DeviceListPage extends GetView<DeviceListController> {
  DeviceListPage({Key? key}) : super(key: key);

  @override
  final String tag = RouterPath.deviceList;

  @override
  DeviceListController get controller =>
      Get.put(DeviceListController(), tag: RouterPath.deviceList);

  @override
  Widget build(BuildContext context) {
    // Log.d('DeviceListPage==');
    return Scaffold(
      appBar: AppBar(title: Text(ResKey.device.tr), actions: [
        PermissionWidget(
            permissionStringList: const [PermissionConstant.deviceAdd],
            child: GestureDetector(
              onTap: controller.toAddDevice,
              child: Padding(
                padding: EdgeInsets.fromLTRB(10.w, 2.h, 20.w, 2.h),
                child: const Icon(
                  Icons.add,
                ),
              ),
            ))
      ]),
      body: SafeArea(
        child: Column(
          children: [
            DecoratedBox(
              decoration: const BoxDecoration(color: Colour.white),
              child: Row(
                children: [
                  Expanded(
                    child: BrnSearchText(
                      maxHeight: 54,
                      innerPadding: const EdgeInsets.fromLTRB(16, 8, 15, 8),
                      hintStyle: TextStyles.lf16,
                      textStyle: TextStyles.f16,
                      innerColor: Colour.backgroundColor,
                      hintText: ResKey.searchDeviceHint.tr,
                      controller: controller.searchTextEditingController,
                      onTextChange: DebounceUtils.debounceInput((p0) {
                        Log.d('onTextChange  ${controller.lock}');
                        if (controller.lock) {
                          return;
                        }
                        controller.pagingState.snOrName = p0;
                        controller.refreshData(isSearch: true);
                      }),
                      onTextClear: () {
                        if (controller.lock) {
                          return false;
                        }
                        controller.pagingState.snOrName = '';
                        controller.refreshData(isSearch: true);
                        controller.isLoad = false;
                        return false;
                      },
                      onTextCommit: (p0) {
                        Log.d('onTextCommit  ${controller.lock}');
                        if (controller.lock) {
                          return;
                        }
                        controller.isLoad = false;
                        controller.pagingState.snOrName = p0;
                        controller.refreshData(isSearch: true);
                      },
                    ),
                  ),
                  GestureDetector(
                    onTap: () => controller.toFilter(context),
                    child: Padding(
                      padding: EdgeInsets.fromLTRB(5.w, 0, 17.w, 0),
                      child: Image.asset(
                        ImageAssets.iconFilter,
                        width: 26.w,
                        height: 26.w,
                      ),
                    ),
                  ),
                ],
              ),
            ),
            Expanded(
              child:
                  buildRefreshListWidget<DeviceListModel, DeviceListController>(
                tag: RouterPath.deviceList,
                itemBuilder: (item, index) => DeviceWidget(
                  controller.pagingState.data[index],
                  onItemTaped: controller.tapDeviceHandler,
                  isRealTime: true,
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
